<?php

namespace app\admin\model\spell;

use think\Db;
use think\Exception;
use think\Model;


class Returned extends Model
{





    // 表名
    protected $name = 'spell_returned';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];
    public function getOriginData()
    {
        return $this->origin;
    }
    protected static function init()
    {
        self::beforeUpdate(function ($row) {
            $changedata = $row->getChangedData();
            $origin = $row->getOriginData();
            //如果有修改审核状态
            if (isset($changedata['status'])) {
                if($changedata['status'] == 2 && @in_array($origin['status'],[1,4])){
                    Db::query("UPDATE `pt_spell_detail` SET returnStatus=2,updatetime=".time()." WHERE id='{$origin['detailId']}' AND returnStatus=1");
                    \app\common\model\User::money($origin['uid'],$origin['returnPrice'],11,"【".$origin['id']."】退款同意返还金额".$origin['returnPrice'],$origin['orderId']."-".$origin['id']);
                }elseif($changedata['status'] == 6){
                    Db::query("UPDATE `pt_spell_detail` SET returnStatus=0,updatetime=".time()." WHERE id='{$origin['detailId']}' AND returnStatus=1");
                }
            }
        });
    }

    /**
     * 退款模式
     */
    public function applyModel()
    {
        return [
            1=>'退货退款',
            2=>'退钱',
        ];
    }
    /**
     * 订单状态
     */
    public function orderStatus()
    {
        return [
            0=>'未选择',
            1=>'未收货',
            2=>'已收货'
        ];
    }
    /**
     * 退款状态
     */
    public function statusInfo()
    {
        return [
            1=>'申请中',
            2=>'已同意',
            3=>'待补充',
            4=>'已补全',
            6=>'已拒绝',
            9=>"已撤销",
            10=>'已删除',
        ];
    }
    /**
     * 物流公司
     */
    public function expressList()
    {
        return [
            "中通快递",
            "圆通速递",
            "韵达快递"
        ];
    }
    /**
     * 申请退换货
     */
    public static function addInfo($orderInfo,$goodsInfo,$info)
    {
        if(empty($info['price']) || $info['price']<=0){
            throw new Exception("退款金额不能为空");
        }
        if ($info['price']>$goodsInfo['price']){
            throw new Exception("退款金额不能超过商品金额");
        }
        if(!@in_array($info['applyModel'],[1,2])){
            throw new Exception("请选择申请类型");
        }
        if($info['applyModel'] == 2 && empty($info['orderStatus'])){
            throw new Exception("请选择收货状态");
        }
        if(empty($info['applyInfo'])){
            throw new Exception("请选择退款原因");
        }
        if(empty($info['info'])){
            throw new Exception("请详细填写申请说明");
        }
        if(empty($info['thumbs'])){
            throw new Exception("请上传图片");
        }
        if(empty($info['mobile'])){
            throw new Exception("请填写联系电话");
        }
        $data = array(
            "uid"=>$orderInfo['uid'],
            "orderId"=>$orderInfo['orderId'],
            "detailId"=>$goodsInfo["id"],
            "goodsId"=>$goodsInfo["goodsId"],
            "skuId"=>$goodsInfo["skuId"],
            "goodsName"=>$goodsInfo["goodsName"],
            "skuName"=>$goodsInfo["skuName"],
            "thumb"=>$goodsInfo["image"],
            "goodsPrice"=>$goodsInfo["price"],
            "returnPrice"=>$info["price"],
            "applyModel"=>intval($info["applyModel"]),
            "orderStatus"=>intval($info["orderStatus"]),
            "applyInfo"=>$info["applyInfo"],
            "info"=>$info["info"],
            "thumbs"=>$info["thumbs"],
            "mobile"=>$info["mobile"],
            "status"=>1,
            "createtime"=>time(),
            "updatetime"=>time(),
        );
        self::insert($data);
    }

    /**
     * 撤销退款申请
     */
    public static function cancelInfo($returnInfo)
    {
        $re = self::where('id',$returnInfo['id'])->where('status','in',[1,3,4])->update([
            "status"=>9,
            "updatetime"=>time(),
        ]);
        if($re == false){
            throw new  Exception("更新失败1");
        }
        Db::query("UPDATE `pt_spell_detail` SET returnStatus=0,updatetime=".time()." WHERE id='{$returnInfo['detailId']}' AND returnStatus=1");
    }
    /**
     * 撤销退款申请
     */
    public static function deleteInfo($returnInfo)
    {
        $re = self::where('id',$returnInfo['id'])->where('status',9)->update([
            "status"=>10,
            "updatetime"=>time(),
        ]);
        if($re == false){
            throw new  Exception("更新失败");
        }
    }
    /**
     * 补全快递信息
     */
    public static function expressInfo($returnInfo,$info)
    {
        if(empty($info['expressName']) || empty($info['expressNo'])){
            throw new Exception("请补全快递信息");
        }
        $re = self::where('id',$returnInfo['id'])->where('status',3)->update([
            "status"=>4,
            "expressName"=>$info["expressName"],
            "expressNo"=>$info["expressNo"],
            "updatetime"=>time(),
        ]);
        if($re == false){
            throw new  Exception("更新失败");
        }
    }






}
